*Table A.26: Mediation Analysis for BMI Z-score (Children)
cd ""

use "master_data_adults_child_s13456_shared.dta", clear
joinby id session using "Supermarket_data_short_S56_EDIandESSEX_STATA.dta", unmatched(both)

drop meal_control snack_control
gen meal_control=1 if treatment==2
replace meal_control=0 if treatment==1

gen snack_control=1 if treatment==3
replace snack_control=0 if treatment==1

drop if type_id=="X" // **Dropping newborns born during the three years of study
drop if id=="3885553B" //**error in the id number and empty data
drop if id=="3885553G" //**error in the id number and empty data
replace treatment=2 if id=="38855553B" //**retrieving the correct treatment for this id
replace treatment=2 if id=="38855553G" //**retrieving the correct treatment for this id
drop if id=="101aa22c" //**this id does not exist
drop if id=="27655663" //**outlier on many measurements


***BMI****
//gen an obesity variable//
*lab define bmi ///
*-1 "Underweight" ///	
*0 "Normal weight" ///	
*1 "Overweight" ///
*2 "Obese", replace

gen obese_var=1 if bmi_cat==2
replace obese_var=0 if obese_var==. & bmi_cat!=.

gen overw_obese=1 if bmi_cat==2 | bmi_cat==1
replace overw_obese=0 if overw_obese==. & bmi_cat!=.

***percentile in children

gen pzheight=normal(zht)
gen pzweight=normal(zwt)

*Calculate z-scores for BMI
egen gendermax=max(gender), by(id_nb)
ge sex=(gendermax==1) 
replace sex=. if gendermax==.

drop zbmi pzbmi
egen zbmi = zanthro(bmi,ba,UK), xvar(months) gender(sex) gencode(male=1, female=0) ageunit(month)
gen pzbmi=normal(zbmi)

ge uwgt_child=(pzbmi<=0.02)
replace uwgt_child=. if pzbmi==.

ge normalw_child=(pzbmi>0.02 & pzbmi<0.85)
replace normalw_child=. if pzbmi==.

ge overw_child=(pzbmi>=0.85 & pzbmi<0.91)
replace overw_child=. if pzbmi==.  

ge overw_obese_child=(pzbmi>=0.85)
replace overw_obese_child=. if pzbmi==.  

ge obesew_child=(pzbmi>=0.91)
replace obesew_child=. if pzbmi==.

drop bmi_cat_child
ge bmi_cat_child=.
replace bmi_cat_child=-1 if uwgt_child==1
replace bmi_cat_child=0 if normalw_child==1
replace bmi_cat_child=1 if overw_child==1
replace bmi_cat_child=2 if obesew_child==1
drop bmi_cat_child

***macro nutrients*****
ge food15_other_veg_new=food15_other_veg
replace food15_other_veg_new=0 if food15_other_veg==. & energykcal!=.

ge food16_fruit_new=food16_fruit
replace food16_fruit_new=0 if food16_fruit==. & energykcal!=.

su food15_other_veg 
su food16_fruit 
su food15_other_veg_new 
su food16_fruit_new

*
drop portion_veg_wisp portion_fruit_wisp
gen portion_veg_wisp=food15_other_veg/80
gen portion_fruit_wisp=food16_fruit/80
gen fuit_and_veg_g=total_veg_fruit_eatwell if session==1
replace fuit_and_veg_g=total_veg_fruit_eatwell if session==3
replace fuit_and_veg_g=food15_other_veg+food16_fruit if session==4

ge FV=food15_other_veg_new+food16_fruit_new

generate calories1=energykcal
generate calories2=shop_calories
generate sugar1=nmesg
generate sugar2=shop_sugar
generate FV1=FV
generate FV2=shop_fruit_veg_spend
generate fat1=saturatesg
generate fat2=shop_fat

keep if child==1
xtset id_nb session

ge meal=treatment==2
ge snack=treatment==3

********************************************************************************
*Create the main point estimates
xi: xtreg zbmi i.meal*i.session i.snack*i.session, fe cl(hh_id)
xi: xtreg zbmi i.meal*i.session i.snack*i.session sugar1, fe cl(hh_id)
xi: xtreg zbmi i.meal*i.session i.snack*i.session fat1, fe cl(hh_id)
xi: xtreg zbmi i.meal*i.session i.snack*i.session FV1, fe cl(hh_id)
********************************************************************************

*Code for the p-values of difference
xi: reg zbmi i.meal*i.session i.snack*i.session i.id_nb
est store base
xi: reg zbmi i.meal*i.session i.snack*i.session sugar1 i.id_nb
est store sugar
xi: reg zbmi i.meal*i.session i.snack*i.session fat1 i.id_nb
est store fat
xi: reg zbmi i.meal*i.session i.snack*i.session FV1 i.id_nb
est store FV

suest base sugar, cl(hh_id)
*Meal
*each session
test [base_mean]_ImeaXses_1_3=[sugar_mean]_ImeaXses_1_3
test [base_mean]_ImeaXses_1_4=[sugar_mean]_ImeaXses_1_4
test [base_mean]_ImeaXses_1_5=[sugar_mean]_ImeaXses_1_5
test [base_mean]_ImeaXses_1_6=[sugar_mean]_ImeaXses_1_6

*all sessions
test [base_mean]_ImeaXses_1_3 + [base_mean]_ImeaXses_1_4 + [base_mean]_ImeaXses_1_5 + [base_mean]_ImeaXses_1_6 = [sugar_mean]_ImeaXses_1_3 + [sugar_mean]_ImeaXses_1_4 + [sugar_mean]_ImeaXses_1_5 + [sugar_mean]_ImeaXses_1_6


*Snack
*each session
test [base_mean]_IsnaXses_1_3=[sugar_mean]_IsnaXses_1_3
test [base_mean]_IsnaXses_1_4=[sugar_mean]_IsnaXses_1_4
test [base_mean]_IsnaXses_1_5=[sugar_mean]_IsnaXses_1_5
test [base_mean]_IsnaXses_1_6=[sugar_mean]_IsnaXses_1_6

*all sessions
test [base_mean]_IsnaXses_1_3 + [base_mean]_IsnaXses_1_4 + [base_mean]_IsnaXses_1_5 + [base_mean]_IsnaXses_1_6 = [sugar_mean]_IsnaXses_1_3 + [sugar_mean]_IsnaXses_1_4 + [sugar_mean]_IsnaXses_1_5 + [sugar_mean]_IsnaXses_1_6

suest base fat, cl(hh_id)
*Meal
*each session
test [base_mean]_ImeaXses_1_3=[fat_mean]_ImeaXses_1_3
test [base_mean]_ImeaXses_1_4=[fat_mean]_ImeaXses_1_4
test [base_mean]_ImeaXses_1_5=[fat_mean]_ImeaXses_1_5
test [base_mean]_ImeaXses_1_6=[fat_mean]_ImeaXses_1_6

*all sessions
test [base_mean]_ImeaXses_1_3 + [base_mean]_ImeaXses_1_4 + [base_mean]_ImeaXses_1_5 + [base_mean]_ImeaXses_1_6 = [fat_mean]_ImeaXses_1_3 + [fat_mean]_ImeaXses_1_4 + [fat_mean]_ImeaXses_1_5 + [fat_mean]_ImeaXses_1_6

*Snack
*each session
test [base_mean]_IsnaXses_1_3=[fat_mean]_IsnaXses_1_3
test [base_mean]_IsnaXses_1_4=[fat_mean]_IsnaXses_1_4
test [base_mean]_IsnaXses_1_5=[fat_mean]_IsnaXses_1_5
test [base_mean]_IsnaXses_1_6=[fat_mean]_IsnaXses_1_6

*all sessions
test [base_mean]_IsnaXses_1_3 + [base_mean]_IsnaXses_1_4 + [base_mean]_IsnaXses_1_5 + [base_mean]_IsnaXses_1_6 = [fat_mean]_IsnaXses_1_3 + [fat_mean]_IsnaXses_1_4 + [fat_mean]_IsnaXses_1_5 + [fat_mean]_IsnaXses_1_6


suest base FV, cl(hh_id)
*Meal
*each session
test [base_mean]_ImeaXses_1_3=[FV_mean]_ImeaXses_1_3
test [base_mean]_ImeaXses_1_4=[FV_mean]_ImeaXses_1_4
test [base_mean]_ImeaXses_1_5=[FV_mean]_ImeaXses_1_5
test [base_mean]_ImeaXses_1_6=[FV_mean]_ImeaXses_1_6

*all sessions
test [base_mean]_ImeaXses_1_3 + [base_mean]_ImeaXses_1_4 + [base_mean]_ImeaXses_1_5 + [base_mean]_ImeaXses_1_6 = [FV_mean]_ImeaXses_1_3 + [FV_mean]_ImeaXses_1_4 + [FV_mean]_ImeaXses_1_5 + [FV_mean]_ImeaXses_1_6

*Snack
*each session
test [base_mean]_IsnaXses_1_3=[FV_mean]_IsnaXses_1_3
test [base_mean]_IsnaXses_1_4=[FV_mean]_IsnaXses_1_4
test [base_mean]_IsnaXses_1_5=[FV_mean]_IsnaXses_1_5
test [base_mean]_IsnaXses_1_6=[FV_mean]_IsnaXses_1_6

*all sessions
test [base_mean]_IsnaXses_1_3 + [base_mean]_IsnaXses_1_4 + [base_mean]_IsnaXses_1_5 + [base_mean]_IsnaXses_1_6 = [FV_mean]_IsnaXses_1_3 + [FV_mean]_IsnaXses_1_4 + [FV_mean]_IsnaXses_1_5 + [FV_mean]_IsnaXses_1_6
